package com.globagames.strategicgames.view.commons.actions
{
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Text;
	import mx.core.BitmapAsset;
	import mx.core.Container;
	[Event(name="actionSelected" , type="flash.events.Event")]
	[Event(name="actionDeselected" , type="flash.events.Event")]
	public class GameActionBar extends Container
	{
		public static var ACTION_MOVE:int = 0;
		public static var ACTION_ATTACK:int = 1;
		public static var ACTION_REMOVE:int = 2;
		public static var ACTION_INFOS:int = 3;
		
		[Bindable]
		public var actionIndex:int;
		
		[Embed(source="/assets/icones.png")]
		private var imgCls:Class;
		
		public var t:Text;
		
		private var bitmap:BitmapData;
		public function GameActionBar()
		{
			super();
			var ba:BitmapAsset = new imgCls() as BitmapAsset;
			bitmap = ba.bitmapData;
			mouseEnabled = false;
			
			
			
			
		}
		override protected function createChildren():void{
			super.createChildren();	
			
			//addEventListener(MouseEvent.CLICK,mouseClickImage);
			
			t = new Text();
			t.x = 0;
			t.y = 4;
			t.includeInLayout = false;
			t.mouseEnabled = false;
			this.addChild(t);
			invalidateProperties();
			
		}
		
		public function addImageAction(ia:int ):void {
			var i:ActionImage = new ActionImage();
			i.iconIndex = ia;
			i.y = t.y - 4;
			i.construct(bitmap);
			i.gab = this;
			this.addChildAt(i,getChildren().length);
			i.addEventListener("actionSelected", actionSelected);
			t.y = i.y + 48 + 4;
			invalidateProperties();
		}
		public function reset():void {
			while(  getChildren().length > 1 ) {
				removeChildAt(1);
			}
			t.y = 4;

		}
		private function actionSelected(event:Event):void 
        { 
        	trace("GameActionBar::actionSelected");
        	var e:Event	= new Event("actionSelected");
	        dispatchEvent(e);
        }
		private function mouseClickImage(event:MouseEvent):void 
        {        	
        	 trace("GameActionBar::mouseClickImage");
        	 //if( event.eventPhase == EventPhase.CAPTURING_PHASE ) {
        	 	//event.stopImmediatePropagation();
	        	 var arr:Array = getChildren();
	        	 for( var i:int = 1; i < arr.length; i++ ) {
	        	 	var ai:ActionImage = arr[i];
	        	 	if( ai != event.target ) {
	        	 		ai.iconStyle = ActionImage.STYLE_NORMAL;         	 		      	 		       	 	
	        	 	} else {
	        	 		if( ai.iconStyle == ActionImage.STYLE_SELECTED ) {
	        	 			actionIndex = ai.iconIndex;
	        	 			t.text = ai.messagesXML.actionlabel[actionIndex];
	        	 			var e:Event	= new Event("actionSelected");
	        	 			dispatchEvent(e);
	        	 		}
	        	 		else {
	        	 			actionIndex = -1;
	        	 			t.text = "";
	        	 			var e:Event	= new Event("actionDeselected");
	        	 			dispatchEvent(e);
	        	 		}
	        	 	}
	        	 }
        	// }
        }
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {			
			super.updateDisplayList(unscaledWidth, unscaledHeight);	
				
		}
		override protected function commitProperties():void {
			super.commitProperties();
			var size:int = getChildren().length - 1;
			width = 48 +2;
			height = 48 * size + 2;
		}	
	}	
}